package br.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import br.com.bean.FuncionarioBean;
import br.com.bean.HorarioBean;

public class HorarioDao {

public void inserir (Connection conn, HorarioBean value) throws Exception{
		
		String sql = "INSERT INTO horario(id_escola, id_cpf, matricula, " +
				"folha, contrato, entrada1, saida1, entrada2, saida2, " +
				"observacao) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, value.getId_escola());
		ps.setString(2, value.getId_cpf().getCpf());
		ps.setInt(3, value.getMatricula());
		ps.setInt(4, value.getFolha());
		ps.setString(5, value.getContrato());
		ps.setString(6, value.getEntrada1());
		ps.setString(7, value.getSaida1());
		ps.setString(8, value.getEntrada2());
		ps.setString(9, value.getSaida2());
		ps.setString(10, value.getObservacao());
		ps.execute();
		ps.close();
		
	}
	
	public void alterar (Connection conn, HorarioBean value) throws Exception{
		
		String sql = "UPDATE horario SET id_escola=?, id_cpf=?, matricula=?, " +
				"folha=?, contrato=?, entrada1=?, saida1=?, entrada2=?, " +
				"saida2=?, observacao=? WHERE id = ?";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, value.getId_escola());
		ps.setString(2, value.getId_cpf().getCpf());
		ps.setInt(3, value.getMatricula());
		ps.setInt(4, value.getFolha());
		ps.setString(5, value.getContrato());
		ps.setString(6, value.getEntrada1());
		ps.setString(7, value.getSaida1());
		ps.setString(8, value.getEntrada2());
		ps.setString(9, value.getSaida2());
		ps.setString(10, value.getObservacao());
		ps.execute();
		ps.close();

	}
	
	public void excluir (Connection conn, HorarioBean value) throws Exception {

		String sql = "DELETE * FROM horario WHERE id = ?";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getId());
		ps.executeUpdate();
	}
	
	public ArrayList<HorarioBean> getLista(Connection conn, HorarioBean value) throws Exception{

		String sql = "SELECT * FROM horario where id = ?";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getId());
		ResultSet rs = ps.executeQuery();
		ArrayList<HorarioBean> na = new ArrayList<HorarioBean>();
		while(rs.next()){
			HorarioBean a = new HorarioBean();
			a.setId_escola(rs.getString("id_escola"));
			a.getId_cpf().setCpf(rs.getString("id_cpf"));
			a.setMatricula(rs.getInt("matricula"));
			a.setFolha(rs.getInt("folha"));
			a.setContrato(rs.getString("contrato"));
			a.setEntrada1(rs.getString("entrada1"));
			a.setSaida1(rs.getString("saida1"));
			a.setEntrada2(rs.getString("entrada2"));
			a.setSaida2(rs.getString("saida2"));
			a.setObservacao(rs.getString("observacao"));
			na.add(a);
		}
		return na;
	}

	public ArrayList<HorarioBean> getLista(Connection conn) throws Exception{

		String sql = "SELECT * FROM horario";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		ArrayList<HorarioBean> na = new ArrayList<HorarioBean>();
		while(rs.next()){
			HorarioBean a = new HorarioBean();
			a.setId_escola(rs.getString("id_escola"));
			a.getId_cpf().setCpf(rs.getString("id_cpf"));
			a.setMatricula(rs.getInt("matricula"));
			a.setFolha(rs.getInt("folha"));
			a.setContrato(rs.getString("contrato"));
			a.setEntrada1(rs.getString("entrada1"));
			a.setSaida1(rs.getString("saida1"));
			a.setEntrada2(rs.getString("entrada2"));
			a.setSaida2(rs.getString("saida2"));
			a.setObservacao(rs.getString("observacao"));
			na.add(a);
		}
		return na;
	}	
	// gerar relatorio geral de horario
	public List<HorarioBean> getListaHrs (Connection conn) throws Exception{
		
		String sql = "select f.nome fnome, f.cpf fcpf, f.endereco fend,h.matricula hmat," +
				"h.folha hfls, h.contrato hcont, h.entrada1 hend1, h.saida1 hsad1, h.entrada2 hend2," +
				"h.saida2 hsad2, h.observacao hobs FROM horario h, cad_funcionario f" +
				"where h.id_cpf=f.cpf";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		List<HorarioBean> lista = new ArrayList<HorarioBean>();
		HorarioBean a = null;
		while (rs.next()){
			a = new HorarioBean(); 
			a.getId_cpf().setNome(rs.getString("fnome"));
			a.getId_cpf().setCpf(rs.getString("fcpf"));
			a.getId_cpf().setEnd(rs.getString("fend"));
			a.setMatricula(rs.getInt("hmat"));
			a.setFolha(rs.getInt("hfls"));
			a.setContrato(rs.getString("hcont"));
			a.setEntrada1(rs.getString("hend1"));
			a.setSaida1(rs.getString("hsad1"));
			a.setEntrada2(rs.getString("hend2"));
			a.setSaida2(rs.getString("hsad2"));
			a.setObservacao(rs.getString("hobs"));
			lista.add(a);
		}
		return lista;
	}
	// gerar relatorio geral de horario especifico
		public List<HorarioBean> getListaHrs (Connection conn, HorarioBean value ) throws Exception{
			
			String sql = "h.id_escola escola, h.matricula hmat, f.cpf fcpf, select f.nome fnome," +
					" h.entrada1 hend1, h.saida1 hsad1, h.entrada2 hend2," +
					"where h.id_cpf=f.cpf and h.id_cpf='?'";
			
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			ps.setString(1, value.getId_cpf().getCpf());
			List<HorarioBean> lista = new ArrayList<HorarioBean>();
			HorarioBean a = null;
			while (rs.next()){
				a = new HorarioBean(); 
				a.setId_escola(rs.getString("escola"));
				a.setMatricula(rs.getInt("hmat"));
				a.getId_cpf().setCpf(rs.getString("fcpf"));
				a.getId_cpf().setNome(rs.getString("fnome"));
				a.setEntrada1(rs.getString("hend1"));
				a.setSaida1(rs.getString("hsad1"));
				a.setEntrada2(rs.getString("hend2"));
				a.setSaida2(rs.getString("hsad2"));
				lista.add(a);
			}
			return lista;
		}
		// gerar relatorio geral de horario especifico
				public List<HorarioBean> getListaHrs1 (Connection conn, HorarioBean value ) throws Exception{
					
					String sql = "select f.nome fnome, f.cpf fcpf, f.endereco fend,h.matricula hmat," +
							"h.folha hfls, h.contrato hcont, h.entrada1 hend1, h.saida1 hsad1, h.entrada2 hend2," +
							"h.saida2 hsad2, h.observacao hobs FROM horario h, cad_funcionario f" +
							"where h.id_cpf=f.cpf and h.id_cpf='?'";
					
					PreparedStatement ps = conn.prepareStatement(sql);
					ResultSet rs = ps.executeQuery();
					ps.setString(1, value.getId_cpf().getCpf());
					List<HorarioBean> lista = new ArrayList<HorarioBean>();
					HorarioBean a = null;
					while (rs.next()){
						a = new HorarioBean(); 
						a.getId_cpf().setNome(rs.getString("fnome"));
						a.getId_cpf().setCpf(rs.getString("fcpf"));
						a.getId_cpf().setEnd(rs.getString("fend"));
						a.setMatricula(rs.getInt("hmat"));
						a.setFolha(rs.getInt("hfls"));
						a.setContrato(rs.getString("hcont"));
						a.setEntrada1(rs.getString("hend1"));
						a.setSaida1(rs.getString("hsad1"));
						a.setEntrada2(rs.getString("hend2"));
						a.setSaida2(rs.getString("hsad2"));
						a.setObservacao(rs.getString("hobs"));
						lista.add(a);
					}
					return lista;
				}
}
